package com.jsb.laundry.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.jsb.laundry.common.exception.*;
import com.jsb.laundry.model.dto.posts.*;
import com.jsb.laundry.model.entity.JsbPostComments;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.model.vo.PostCommentVO;
import com.jsb.laundry.model.vo.PostsVO;
import com.jsb.laundry.service.JsbPostCommentsService;
import com.jsb.laundry.service.JsbPostsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帖子接口
 */
@RestController
@RequestMapping("/posts/comment")
@Api(value = "帖子相关接口", tags = "帖子")
@Slf4j
public class PostCommentController {

    @Resource
    private JsbPostsService postService;

    @Resource
    private JsbPostCommentsService postCommentService;

    private final static Gson GSON = new Gson();

    // region 增删改查

    /**
     * 创建
     *
     * @param postCommentAddRequest
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加帖子评论")
    public BaseResponse<Long> addPostComment(@RequestBody PostCommentAddRequest postCommentAddRequest) {
        if (postCommentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //参数校验
        if (StringUtils.isAnyBlank(postCommentAddRequest.getUserId(), postCommentAddRequest.getContent())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (postCommentAddRequest.getPostId() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "帖子id有误");
        }
        // 判断是否存在
        JsbPosts post = postService.getById(postCommentAddRequest.getPostId());
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        int result = postCommentService.doAddPostComment(postCommentAddRequest.getPostId(), postCommentAddRequest.getUserId(), postCommentAddRequest.getContent());
        return ResultUtils.success((long)result);
    }

    /**
     * 列表
     *
     * @param postCommentQueryRequest
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("帖子评论列表")
    public BaseResponse<Page<PostCommentVO>> addPost(@RequestBody PostCommentQueryRequest postCommentQueryRequest, HttpServletRequest request) {
        if (postCommentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //参数校验
        if (postCommentQueryRequest.getPostId() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "帖子id有误");
        }
        if ("".equals(postCommentQueryRequest.getLoginUserId())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "登录id不能为空");
        }
        // 判断是否存在
        JsbPosts post = postService.getById(postCommentQueryRequest.getPostId());
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        long current = postCommentQueryRequest.getPageNum();
        long size = postCommentQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<JsbPostComments> postPage = postCommentService.page(new Page<>(current, size),
                postCommentService.getQueryWrapper(postCommentQueryRequest));
        return ResultUtils.success(postCommentService.getPostCommentPage(postCommentQueryRequest.getLoginUserId(), postPage, request));
    }

}
